package definite.Guide.Book.ProgressMonitor.ProgressBar;

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.ProgressMonitor;
import javax.swing.Timer;

public class SampleProgress {

	static ProgressMonitor monitor;
	static int progress;
	static Timer timer;

	static class ProgressMonitorHandler implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (monitor == null)
				return;
			if (monitor.isCanceled()) {
				System.out.println("Monotor canceled");
				timer.stop();
			} else {
				progress += 3;
				monitor.setProgress(progress);
				monitor.setNote("Loading " + progress + " files");
			}
		}

	}

	public static void main(String[] args) {
		Runnable r = new Runnable() {

			@Override
			public void run() {
				JFrame.setDefaultLookAndFeelDecorated(true);
				JFrame frame = new JFrame("Progress Monitor Sample");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setLayout(new GridLayout(0, 1));

				// define start button
				JButton btnStart = new JButton("Start");
				ActionListener startListener = new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						Component parent = (Component) e.getSource();
						ProgressMonitor monitor =
							new ProgressMonitor(parent, "Loading Progress",
									"Getting started...", 0, 2000);
						progress = 0;
					}
				}; // end action

				btnStart.addActionListener(startListener);
				frame.add(btnStart);
				// end button start

				// define manual increase monitor
				// press this button to inscrease progress by 5
				JButton btnMan = new JButton("Manual Increase");
				ActionListener manListener = new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						if (monitor == null)
							return;
						else if (monitor.isCanceled())
							System.out.println("Monitor canceled");
						else {
							progress += 5;
							monitor.setProgress(progress);
							monitor.setNote("Loading " + progress + " files");
						}
					}
				}; // end action

				btnMan.addActionListener(manListener);
				frame.add(btnMan);
				// end of manual increase button
				// define automatic increase button
				JButton btnAuto = new JButton("Auto Increase");
				ActionListener autoListener = new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						if (monitor != null) {
							if (timer == null)
								timer =
									new Timer(20, new ProgressMonitorHandler());
							timer.start();
						}
					}
				};
				btnAuto.addActionListener(autoListener);
				frame.add(btnAuto);
				// end btn Auto
				frame.setLocationRelativeTo(null);
				frame.setResizable(false);
				frame.setSize(300, 200);
				frame.setVisible(true);
			} // end run
		};

		EventQueue.invokeLater(r);

	}
}
